package com.frcdi.qms.system.service.impl;

import com.frcdi.qms.common.constant.UserConstants;
import com.frcdi.qms.common.core.domain.entity.SysPost;
import com.frcdi.qms.common.enums.BusinessType;
import com.frcdi.qms.common.exception.ServiceException;
import com.frcdi.qms.common.utils.StringUtils;
import com.frcdi.qms.system.domain.SysUserOperLog;
import com.frcdi.qms.system.mapper.SysPostMapper;
import com.frcdi.qms.system.mapper.SysUserOperLogMapper;
import com.frcdi.qms.system.mapper.SysUserPostMapper;
import com.frcdi.qms.system.service.ISysPostService;
import com.frcdi.qms.utils.LogManage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 岗位信息 服务层处理
 *
 * @author cell
 */
@Service
public class SysPostServiceImpl implements ISysPostService
{
    @Autowired
    private SysPostMapper postMapper;

    @Autowired
    private SysUserPostMapper userPostMapper;

    @Autowired
    private SysUserOperLogMapper operLogMapper;


    /**
     * 查询岗位信息集合
     *
     * @param post 岗位信息
     * @return 岗位信息集合
     */
    @Override
    public List<SysPost> selectPostList(SysPost post)
    {
        return postMapper.selectPostList(post);
    }

    /**
     * 查询所有岗位
     *
     * @return 岗位列表
     */
    @Override
    public List<SysPost> selectPostAll()
    {
        return postMapper.selectPostAll();
    }

    /**
     * 通过岗位ID查询岗位信息
     *
     * @param postId 岗位ID
     * @return 角色对象信息
     */
    @Override
    public SysPost selectPostById(Long postId)
    {
        return postMapper.selectPostById(postId);
    }

    /**
     * 根据用户ID获取岗位选择框列表
     *
     * @param userId 用户ID
     * @return 选中岗位ID列表
     */
    @Override
    public List<Long> selectPostListByUserId(Long userId)
    {
        return postMapper.selectPostListByUserId(userId);
    }

    /**
     * 校验岗位名称是否唯一
     *
     * @param post 岗位信息
     * @return 结果
     */
    @Override
    public boolean checkPostNameUnique(SysPost post)
    {
        Long postId = StringUtils.isNull(post.getPostId()) ? -1L : post.getPostId();
        SysPost info = postMapper.checkPostNameUnique(post.getPostName());
        if (StringUtils.isNotNull(info) && info.getPostId().longValue() != postId.longValue())
        {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    /**
     * 校验岗位编码是否唯一
     *
     * @param post 岗位信息
     * @return 结果
     */
    @Override
    public boolean checkPostCodeUnique(SysPost post)
    {
        Long postId = StringUtils.isNull(post.getPostId()) ? -1L : post.getPostId();
        SysPost info = postMapper.checkPostCodeUnique(post.getPostCode());
        if (StringUtils.isNotNull(info) && info.getPostId().longValue() != postId.longValue())
        {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    /**
     * 通过岗位ID查询岗位使用数量
     *
     * @param postId 岗位ID
     * @return 结果
     */
    @Override
    public int countUserPostById(Long postId)
    {
        return userPostMapper.countUserPostById(postId);
    }

    /**
     * 删除岗位信息
     *
     * @param postId 岗位ID
     * @return 结果
     */
    @Override
    @Transactional
    public int deletePostById(Long postId)
    {
        SysPost oldResult = selectPostById(postId);
        // 日志记录
        SysUserOperLog operLog = LogManage.getLog("系统管理", "岗位管理", BusinessType.DELETE.ordinal());
        operLog.setOperBefore(StringUtils.getFieldAnnotation(oldResult));
        operLogMapper.insertSysUserOperLog(operLog);

        return postMapper.deletePostById(postId);
    }

    /**
     * 批量删除岗位信息
     *
     * @param postIds 需要删除的岗位ID
     * @return 结果
     */
    @Override
    @Transactional
    public int deletePostByIds(Long[] postIds)
    {
        StringBuilder result = new StringBuilder();
        for (Long postId : postIds)
        {
            SysPost post = selectPostById(postId);
            if (countUserPostById(postId) > 0)
            {
                throw new ServiceException(String.format("%1$s已分配,不能删除", post.getPostName()));
            }
            result.append(StringUtils.getFieldAnnotation(post));
            result.append(";");
        }
        // 日志记录
        SysUserOperLog operLog = LogManage.getLog("系统管理", "岗位管理", BusinessType.DELETE.ordinal());
        operLog.setOperBefore(String.valueOf(result.deleteCharAt(result.length()-1)));
        operLogMapper.insertSysUserOperLog(operLog);

        return postMapper.deletePostByIds(postIds);
    }

    /**
     * 新增保存岗位信息
     *
     * @param post 岗位信息
     * @return 结果
     */
    @Override
    @Transactional
    public int insertPost(SysPost post)
    {
        // 日志记录
        SysUserOperLog operLog = LogManage.getLog("系统管理", "岗位管理", BusinessType.INSERT.ordinal());
        operLog.setOperAfter(StringUtils.getFieldAnnotation(selectPostById(post.getPostId())));
        operLogMapper.insertSysUserOperLog(operLog);

        return postMapper.insertPost(post);
    }

    /**
     * 修改保存岗位信息
     *
     * @param post 岗位信息
     * @return 结果
     */
    @Override
    @Transactional
    public int updatePost(SysPost post)
    {
        SysPost oldPost = selectPostById(post.getPostId());
        int row = postMapper.updatePost(post);
        // 日志记录
        SysUserOperLog operLog = LogManage.getLog("系统管理", "岗位管理", BusinessType.UPDATE.ordinal());
        operLog.setOperBefore(StringUtils.getFieldAnnotation(oldPost));
        operLog.setOperAfter(StringUtils.getFieldAnnotation(selectPostById(post.getPostId())));
        operLogMapper.insertSysUserOperLog(operLog);

        return row;
    }
}
